//
//  RXStatusCell.swift
//  weibo
//
//  Created by uncle-R on 15/12/20.
//  Copyright © 2015年 uncle-R. All rights reserved.
//

import UIKit


class RXStatusCell: UITableViewCell {

    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    
    // cell显示内容需要的模型
    var status: RXStatus? {
        didSet {
            //当控制器给cell 设置模型后 顶部视图显示数据也是依赖模型
            topView.status = status
            contentLabel.text = status?.text
            pictureView.status = status
            let size = pictureView.calcViewSize()
            pictureViewHeightCon?.constant = size.height
            pictureViewWidthCon?.constant = size.width
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //准备UI
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   func prepareUI(){
    
    contentView.addSubview(topView)
    contentView.addSubview(contentLabel)
    contentView.addSubview(bottomView)
    contentView.addSubview(pictureView)
    //添加顶部view的约束
    // 2. 添加约束
    topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
    
    // 微博文本
    contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
    
    // 微博文本宽度约束
    contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
    
    // 微博配图
    let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
    
    // 获取微博配图视图的宽高约束
    // 注意: 设置约束的时候需要设置 size， 不然框架不会添加宽高约束,拿不到宽高约束
    pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
    pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
    // 底部视图
    bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
    
    contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    }
    /// 顶部视图
    private lazy var topView: RXTopView = RXTopView()
    //文字视图
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        //设置字体大小
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        //换行
        label.numberOfLines = 0
        return label
    }()
    //底部视图
    private lazy var bottomView :RXStatusBottomView = RXStatusBottomView()
    
    private lazy var pictureView: RXPictureView = RXPictureView()
//    //配图内容
//    private lazy var pictureView: RXPictureView = RXPictureView()
    
}
    